//
//  TFAlertView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/10.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFAlertView: UIButton {
    //确定
    @objc var resultIndex:touchesBeganBlock?
    //取消
    @objc var cancelIndex:touchesBeganBlock?
    
    @objc let cancelBtn = UIButton()
    @objc let sureBtn = UIButton()
    let titleLab = UILabel()

    let alertView = UIView()
    let lineH = UILabel()
    let messageLab = UILabel()

    func passcgrect(_ rect:CGRect,size:CGSize)->CAShapeLayer
    {
        
        let bezierPath = UIBezierPath.init(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft,UIRectCorner.bottomLeft,.topRight,.bottomRight], cornerRadii: size)
        
      
            let shaper = CAShapeLayer()
            shaper.frame = rect
            shaper.path = bezierPath.cgPath
        return shaper
    }

    @objc public func title(_ title:String,meaasge:String,sureTitle:String,cancleTitle:String)

    {
            let AlertW:CGFloat = 300
        
            self.frame = TalkfunSharedTools.getMainScreen()
        
            self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
          
            self.alertView.frame = CGRect(x: 0, y: 0, width: AlertW, height: 160)
     
            self.alertView.backgroundColor = UIColor.white
        
        
            self.alertView.layer.mask = self.passcgrect(self.alertView.bounds, size: CGSize(width: 6.0, height: 6.0))
            
            self.alertView.layer.position = self.center
            
        if (title.count>0) {
        
                self.titleLab.frame = CGRect(x: 27.5, y: 30, width: AlertW-32.5-27.5, height: 50)
                self.titleLab.textAlignment = .center
                
                self.titleLab.font = UIFont.PingFang(size: 15)
               
                self.titleLab.textColor = UIColor(red: 47/255.0, green: 52/255.0, blue: 58/255.0, alpha: 1)
                self.titleLab.text = title
                self.titleLab.numberOfLines = 2
                self.alertView.addSubview(self.titleLab)
            }
            
        if (meaasge.count>0) {
                
                self.messageLab.frame = CGRect(x: 27.5, y: 26, width:  AlertW-32.5-27.5, height: 25)
                self.messageLab.textAlignment = .center
                self.messageLab.font = UIFont.PingFang(size: 18)
                self.messageLab.textColor = self.titleLab.textColor
                self.messageLab.text = meaasge
                self.alertView.addSubview(self.messageLab)
               self.titleLab.frame = CGRect(x: 27.5, y: 51, width: AlertW-32.5-27.5, height: 49)
               
            } else {
               self.titleLab.frame = CGRect(x: 27.5, y: 30, width: AlertW-32.5-27.5, height: 50)
              
            }
            
            let  lineW = UILabel()
            lineW.frame = CGRect(x: 0, y: 110, width: AlertW, height: 0.5)
      
            lineW.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1)
       
        self.alertView.addSubview(lineW)
            
       
            self.lineH.frame = CGRect(x: (AlertW-0.5)/2, y: 110, width: 0.5, height: 50)
   
            self.lineH.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1)
        self.alertView.addSubview(self.lineH)
            
            
        if (cancleTitle.count>0) {

                self.cancelBtn.frame = CGRect(x: 0, y: 110, width: 149, height: 50)
                self.cancelBtn.setTitle(cancleTitle, for: .normal)
                self.cancelBtn.setTitleColor(UIColor(red: 47/255.0, green: 52/255.0, blue: 58/255.0, alpha: 1), for: .normal)
                
                self.cancelBtn.tag = 2001
                self.cancelBtn.addTarget(self, action: #selector(cancelBtnUI), for: .touchUpInside)
               
                self.alertView.addSubview(self.cancelBtn)
            }
            
        if (sureTitle.count>0) {
                
                self.sureBtn.frame = CGRect(x: 151, y: 110, width: 149, height: 50)
                self.sureBtn.setTitle(sureTitle, for: .normal)
                self.sureBtn.setTitleColor(UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1), for: .normal)
              
                self.sureBtn.tag = 2002
                self.sureBtn.addTarget(self, action: #selector(sureBtnUI), for: .touchUpInside)
              
                
                self.alertView.addSubview(self.sureBtn)
            }
            
        if (sureTitle.count > 0 && cancleTitle.count == 0) {
                self.lineH.isHidden = true
                self.sureBtn.frame = CGRect(x: 0, y: 110, width: AlertW, height: 50)
            
            } else {
                self.sureBtn.frame = CGRect(x: 151, y: 110, width: 149, height: 50)
               
                self.lineH.isHidden = false
            }
            
        self.addSubview(self.alertView)
            
     
  
    }
    //确定
   @objc func sureBtnUI()

    {
        if (self.resultIndex != nil) {
            self.resultIndex!()
        }
        self.removeFromSuperview()
    }
    //取消
   @objc func cancelBtnUI()
 
    {
        if (self.cancelIndex != nil) {
            self.cancelIndex!()
        }
        self.removeFromSuperview()
        
    }

    @objc func showAlertView(_ view:UIView)
    {
        view.addSubview(self)
        self.alertView.layer.position = self.center
        
        
        self.alertView.transform = CGAffineTransform.init(scaleX: 0.90, y: 0.90)
        
        UIView.animate(withDuration: 0.25) {
            self.alertView.transform = CGAffineTransform.init(scaleX: 1, y: 1)
        }
    }
    
}
